package com.yonyou.cyxdms.basedata.service.controller.color;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yonyou.cyx.framework.controller.base.BaseController;
import com.yonyou.cyx.function.utils.common.StringUtils;
import com.yonyou.cyxdms.basedata.service.dto.CarparamcolorDTO;
import com.yonyou.cyxdms.basedata.service.dto.ProductDTO;
import com.yonyou.cyxdms.basedata.service.entity.color.ColorPO;
import com.yonyou.cyxdms.basedata.service.service.color.CarparamcolorService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * CarparamcolorController
 *
 * @author zhangsx
 * @since 2018-09-07
 */
@RestController
@RequestMapping("/api/carparamcolor/carparamcolor")
public class CarparamcolorController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(CarparamcolorController.class);

    @Autowired
    public CarparamcolorService carParamColorPoService;

    /**
     * 分页查询数据
     *
     * @param queryCondition 查询条件
     * @return com.baomidou.mybatisplus.core.metadata.IPage 分页对象
     * @author zhangsx
     * @since 2018-09-07
     */
    @ResponseBody
    @GetMapping("/")
    public IPage<Map<String, Object>> getList(@RequestParam Map<String, Object> queryCondition) {
        Page<Map<String, Object>> page = new Page(1, 2);
        return carParamColorPoService.selectPageBysql(page, queryCondition);
    }

    /**
     * 进行数据修改
     *
     * @param id 数据主键ID
     * @return Map<String                                                                                                                               ,                                                                                                                               Object> 数据对应的map 结构
     * @author zhangsx
     * @since 2018-09-07
     */
    @ResponseBody
    @GetMapping(value = "/{id}")
    public Map<String, Object> getById(@PathVariable("id") Integer id) {
        return carParamColorPoService.getById(id);
    }

    /**
     * product进行数据新增//(先添加一些字段跑同流程，后续完善)
     *
     * @param productDTO 需要保存的DTO
     * @return Map<String                                                               ,                                                               Object> 返回的数据map
     * @author zhangsx
     * @since 2018-09-07
     */
    @ResponseBody
    @PostMapping(value = "/product")
    @ResponseStatus(HttpStatus.CREATED)
    public Map<String, Object> insert(@RequestBody ProductDTO productDTO) {
        logger.debug("--------------------进入product存储阶段--------------");
        logger.debug("productDTO:{}", productDTO);
        Map<String, Object> map = null;
        //接收productDTO参数的值
        CarparamcolorDTO carparamcolorDTO = new CarparamcolorDTO();
        carparamcolorDTO.setProductCode(productDTO.getProductCode());
        carparamcolorDTO.setProductName(productDTO.getProductName());
        carparamcolorDTO.setCarId(productDTO.getPackageId());
        carparamcolorDTO.setColorCode(productDTO.getColor());
        carparamcolorDTO.setColorName(productDTO.getColorName());
        //productStatus字段三菱现在不传
        carparamcolorDTO.setIsValid(productDTO.getIsValid());
        //oemDirectivePrice字段三菱现在不传
        if (!StringUtils.isNullOrEmpty(productDTO.getDirectivePrice())) {
            carparamcolorDTO.setReferPrice(productDTO.getDirectivePrice());
        }
        //exeuntDate字段现在不传
        //remark字段现在不传
        //vhclPrice字段现在不传
        //addPrice字段现在不传
        //minPrice字段现在不传
        carparamcolorDTO.setTrimCode(productDTO.getTrimCode());
        carparamcolorDTO.setTrimColorName(productDTO.getTrimColorName());
        carparamcolorDTO.setSourceId(productDTO.getSourceId());
        carparamcolorDTO.setSourceChannel(productDTO.getSourceChannel());
        carparamcolorDTO.setAppId("ff7555e8ac239560");
        if ("add".equals(productDTO.getBizId())) {
            logger.debug("------------product新增数据-----------------");
            map = carParamColorPoService.insert(carparamcolorDTO);
        } else if ("update".equals(productDTO.getBizId())) {
            ColorPO carParamColorPo = new ColorPO();
            carparamcolorDTO.setSourceChannel(productDTO.getSourceChannel());
            carparamcolorDTO.setAppId("ff7555e8ac239560");
            ColorPO carParamColorPos = carParamColorPoService.getOne(new QueryWrapper<>(carParamColorPo));
            carParamColorPoService.update(carParamColorPos.getColorId(), carparamcolorDTO);
        }

        return map;
    }

    /**
     * 进行数据修改
     *
     * @param id               需要修改数据的ID
     * @param carparamcolorDTO 需要保存的DTO
     * @return Map<String                                                                                                                               ,                                                                                                                               Object> 返回的数据map
     * @author zhangsx
     * @since 2018-09-07
     */
    @ResponseBody
    @PutMapping(value = "/{id}")
    @ResponseStatus(HttpStatus.CREATED)
    public Map<String, Object> update(@PathVariable("id") Integer id, @RequestBody CarparamcolorDTO carparamcolorDTO) {
        return carParamColorPoService.update(id, carparamcolorDTO);
    }

    /**
     * 查询list
     *
     * @param queryCondition 查询条件
     * @return java.util.List<java.util.Map                               <                               java.lang.String                               ,                               java.lang.Object>>
     * @author guorp1
     * @since 2018/10/9
     */
    @ResponseBody
    @GetMapping("/selectListBySql")
    public List<Map<String, Object>> selectListBySql(@RequestParam Map<String, Object> queryCondition) {
        return carParamColorPoService.selectListBySql(queryCondition);
    }

    /**
     * 查询颜色信息
     *
     * @param queryCondition
     * @return java.util.Map<java.lang.String                               ,                               java.lang.Object>
     * @author renwd
     * @since 2018/11/26
     */
    @GetMapping("/queryColorByCondition")
    public Map<String, Object> queryColorByCondition(@RequestParam Map<String, Object> queryCondition) {
        return carParamColorPoService.queryColorByCondition(queryCondition);
    }
}